package org.jsola.hr.web;

import org.jsola.core.Page;
import org.jsola.core.Result;
import org.jsola.exception.ParamException;
import org.jsola.hr.constant.HrConstants;
import org.jsola.hr.dto.SignUpAddDTO;
import org.jsola.hr.dto.SignUpListUpdateDTO;
import org.jsola.hr.dto.SignUpUpdateDTO;
import org.jsola.hr.query.SignUpQuery;
import org.jsola.hr.service.ISignUpService;
import org.jsola.hr.vo.SignUpListVO;
import org.jsola.hr.vo.SignUpVO;
import org.jsola.user.core.TokenUser;
import org.jsola.user.core.UserContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import javax.validation.Valid;


/**
 * 公司首页报名表
 *
 * @author zhr
 */
@RestController("hrSignUpController")
@RequestMapping(HrConstants.API_PATH + "/v1/signUps")
public class SignUpController implements ISignUpControllerAPI {

    @Autowired
    private ISignUpService signUpService;

    @Override
    @PostMapping
    public Result<SignUpVO> save(@Valid @RequestBody SignUpAddDTO signUpAddDTO) {
        TokenUser tokenUser = new TokenUser();
        tokenUser.setSiteId("");
        tokenUser.setUserId("");
        // todo 新增公司首页报名表权限校验

        return Result.success(
                signUpService.save(signUpAddDTO, tokenUser)
        );
    }


    @Override
    @PutMapping
    public Result<Integer> update(@Valid @RequestBody SignUpUpdateDTO signUpUpdateDTO) {
        TokenUser tokenUser = new TokenUser();
        tokenUser.setSiteId("");
        tokenUser.setUserId("");
        // todo 修改公司首页报名表权限校验

        int count = signUpService.update(signUpUpdateDTO, tokenUser);
        if (count == 0) {
            throw new ParamException("更新失败，请刷新页面后重试");
        }
        return Result.success(count);
    }

    @Override
    @PutMapping(value = "/updateList")
    public Result<Integer> updateList(@Valid @RequestBody SignUpListUpdateDTO signUpListUpdateDTO) {
        TokenUser tokenUser = new TokenUser();
        tokenUser.setSiteId("");
        tokenUser.setUserId("");
        // todo 修改公司首页报名表权限校验

        int count = signUpService.updateList(signUpListUpdateDTO, tokenUser);
        if (count == 0) {
            throw new ParamException("更新失败，请刷新页面后重试");
        }
        return Result.success(count);
    }


    @Override
    @DeleteMapping(value = "/{signUpId}")
    public Result<Integer> delete(@PathVariable String signUpId) {
        TokenUser tokenUser = new TokenUser();
        tokenUser.setSiteId("");
        tokenUser.setUserId("");
        // todo 彻底删除公司首页报名表权限校验

        int count = signUpService.deleteByIds(tokenUser, signUpId);
        if (count == 0) {
            throw new ParamException("删除失败，请刷新页面后重试");
        }
        return Result.success(count);
    }

    @Override
    @DeleteMapping
    public Result<Integer> deleteBatch(@RequestBody String[] signUpIds) {
        TokenUser tokenUser = new TokenUser();
        tokenUser.setSiteId("");
        tokenUser.setUserId("");
        // todo 批量彻底删除公司首页报名表权限校验

        int count = signUpService.deleteByIds(tokenUser, signUpIds);
        if (count == 0) {
            throw new ParamException("删除失败，请刷新页面后重试");
        }
        return Result.success(count);
    }

    @Override
    @GetMapping(value = "/{signUpId}")
    public Result<SignUpVO> findById(@PathVariable String signUpId) {
        TokenUser tokenUser = new TokenUser();
        tokenUser.setSiteId("");
        tokenUser.setUserId("");
        // todo 查看公司首页报名表权限校验

        return Result.success(
            signUpService.selectById(signUpId, tokenUser.getSiteId())
        );
    }

    @Override
    @GetMapping(value = "/listAll")
    public Result<List<SignUpListVO>> listAll(@Valid SignUpQuery signUpQuery) {
        TokenUser tokenUser = new TokenUser();
        tokenUser.setSiteId("");
        tokenUser.setUserId("");
        // todo 查看公司首页报名表权限校验

        return Result.success(
            signUpService.select(signUpQuery, tokenUser.getSiteId())
        );
    }


    @Override
    @GetMapping
    public Result<Page<SignUpListVO>> page(@Valid SignUpQuery signUpQuery) {
        TokenUser tokenUser = new TokenUser();
        tokenUser.setSiteId("");
        tokenUser.setUserId("");
        // todo 查看项目权限校验

        return Result.success(
            signUpService.selectPage(signUpQuery, tokenUser.getSiteId())
        );
    }


}

